package org.jeecg.modules.wlhy.bill.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.wlhy.bill.dto.HyCarrierBillDTO;
import org.jeecg.modules.wlhy.bill.entity.HyCarrierBill;
import org.jeecg.modules.wlhy.bill.service.IHyCarrierBillService;
import org.jeecg.modules.wlhy.setting.service.IHySettingService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.DataMaskingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

 /**
 * @Description: hy_carrier_bill
 * @Author: jeecg-boot
 * @Date:   2021-03-15
 * @Version: V1.0
 */
@Api(tags="hy_carrier_bill")
@RestController
@RequestMapping("/bill/hyCarrierBill")
@Slf4j
public class HyCarrierBillController extends JeecgController<HyCarrierBill, IHyCarrierBillService> {
	@Autowired
	private IHyCarrierBillService hyCarrierBillService;

	@Autowired
	IHyShippingNoteService hyShippingNoteService;

	 @Resource
	 private IHySettingService settingService;
//	 @Autowired
//	 private UserUtil userUtil;

	/**
	 * 分页列表查询
	 *
	 * @param hyCarrierBill
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="hy_carrier_bill-分页列表查询", notes="hy_carrier_bill-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyCarrierBill hyCarrierBill,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		String shippingNoteNumber = req.getParameter("shippingNoteNumber");
		QueryWrapper<HyCarrierBill> queryWrapper = QueryGenerator.initQueryWrapper(hyCarrierBill, req.getParameterMap());
		if(StringUtils.isNotEmpty(shippingNoteNumber)){
			try {
				HyShippingNote shippingNote = hyShippingNoteService.queryByShippingNoteNumber(shippingNoteNumber);
				if (shippingNote != null) {
					queryWrapper.eq("shipping_id", shippingNote.getId());
				} else {
					queryWrapper.eq("shipping_id", "-1");
				}
			}catch(Exception e){
				queryWrapper.eq("shipping_id", "-1");
			}
		}
		Page<HyCarrierBill> page = new Page<HyCarrierBill>(pageNo, pageSize);
		IPage<HyCarrierBill> pageList = hyCarrierBillService.page(page, queryWrapper);

		//如果开启系统脱敏
		String dataMaskingFlag = this.settingService.getSettingValueByTag("data_masking");
		if("1".equals(dataMaskingFlag)) {
			for (HyCarrierBill dto : pageList.getRecords()) {
				dto.setBillNum(DataMaskingUtil.bankNumberMasking(dto.getBillNum()));
				dto.setCraccno(DataMaskingUtil.bankNumberMasking(dto.getCraccno()));
				dto.setDbaccno(DataMaskingUtil.bankNumberMasking(dto.getDbaccno()));
			}
		}

		return Result.OK(pageList);
	}
	 /**
//	  * 新查询接口
//	  */
	 @GetMapping(value = "/newList")
	 public Result<?> queryPgList(HyCarrierBillDTO hyCarrierBill,
								@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest request){


		 String shippingNoteNumber = request.getParameter("shippingNoteNumber");
		QueryWrapper<HyCarrierBillDTO> queryWrapper = QueryGenerator.initQueryWrapper(hyCarrierBill, request.getParameterMap());

		 if(StringUtils.isNotEmpty(shippingNoteNumber)){
			 try {
				 HyShippingNote shippingNote = hyShippingNoteService.queryByShippingNoteNumber(shippingNoteNumber);
				 if (shippingNote != null) {
					 queryWrapper.eq("shipping_id", shippingNote.getId());
				 } else {
					 queryWrapper.eq("shipping_id", "-1");
				 }
			 }catch(Exception e){
				 queryWrapper.eq("shipping_id", "-1");
			 }
		 }
		 queryWrapper.isNotNull("hyb.id");
		 queryWrapper.orderByDesc("hyb.create_time");
		 Page<HyCarrierBillDTO> page = new Page<HyCarrierBillDTO>(pageNo, pageSize);
		 IPage<HyCarrierBillDTO> pageList = hyCarrierBillService.queryPgList(page, queryWrapper);

		 return Result.OK(pageList);



	 }


//	/**
//	 *   添加
//	 *
//	 * @param hyCarrierBill
//	 * @return
//	 */
//	@AutoLog(value = "hy_carrier_bill-添加")
//	@ApiOperation(value="hy_carrier_bill-添加", notes="hy_carrier_bill-添加")
//	@PostMapping(value = "/add")
//	public Result<?> add(@RequestBody HyCarrierBill hyCarrierBill) {
//		hyCarrierBillService.save(hyCarrierBill);
//		return Result.OK("添加成功！");
//	}

	/**
	 *  编辑
	 *
	 * @param hyCarrierBill
	 * @return
	 */
	@AutoLog(value = "hy_carrier_bill-编辑")
	@ApiOperation(value="hy_carrier_bill-编辑", notes="hy_carrier_bill-编辑")
	@PutMapping(value = "/edit")
	@RequiresPermissions("carrierbill:edit")
	public Result<?> edit(@RequestBody HyCarrierBill hyCarrierBill) {
		HyCarrierBill entity = hyCarrierBillService.getById(hyCarrierBill.getId());
		if(entity==null){
			return Result.error("数据不存在!");
		}
		String craccname = hyCarrierBill.getCraccname();
		if(StringUtils.isEmpty(craccname)){
			return Result.error("数据不能为空!");
		}
		String craccno = hyCarrierBill.getCraccno();
		if(StringUtils.isEmpty(craccno)){
			return Result.error("数据不能为空!");
		}
		entity.setCraccname(hyCarrierBill.getCraccname());
		entity.setCraccno(hyCarrierBill.getCraccno());
		hyCarrierBillService.updateById(entity);
		return Result.OK("编辑成功!");
	}

//	/**
//	 *   通过id删除
//	 *
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "hy_carrier_bill-通过id删除")
//	@ApiOperation(value="hy_carrier_bill-通过id删除", notes="hy_carrier_bill-通过id删除")
//	@DeleteMapping(value = "/delete")
//	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
//		hyCarrierBillService.removeById(id);
//		return Result.OK("删除成功!");
//	}
//
//	/**
//	 *  批量删除
//	 *
//	 * @param ids
//	 * @return
//	 */
//	@AutoLog(value = "hy_carrier_bill-批量删除")
//	@ApiOperation(value="hy_carrier_bill-批量删除", notes="hy_carrier_bill-批量删除")
//	@DeleteMapping(value = "/deleteBatch")
//	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
//		this.hyCarrierBillService.removeByIds(Arrays.asList(ids.split(",")));
//		return Result.OK("批量删除成功!");
//	}
//
//	/**
//	 * 通过id查询
//	 *
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "hy_carrier_bill-通过id查询")
//	@ApiOperation(value="hy_carrier_bill-通过id查询", notes="hy_carrier_bill-通过id查询")
//	@GetMapping(value = "/queryById")
//	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
//		HyCarrierBill hyCarrierBill = hyCarrierBillService.getById(id);
//		if(hyCarrierBill==null) {
//			return Result.error("未找到对应数据");
//		}
//		return Result.OK(hyCarrierBill);
//	}
//
    /**
    * 导出excel
    *
    * @param request
    * @param hyCarrierBill
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HyCarrierBill hyCarrierBill) {
        return super.exportXls(request, hyCarrierBill, HyCarrierBill.class, "代收人流水");
    }
//
//    /**
//      * 通过excel导入数据
//    *
//    * @param request
//    * @param response
//    * @return
//    */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//        return super.importExcel(request, response, HyCarrierBill.class);
//    }

}
